#include "mesh_skin.h"
#include <lampe/xml/IXMLFactoryEnvironment.h>
#include "irrxml.h"
#include <lampe/xml/IXmlNode.h>
#include <ISceneNodeAnimator.h>
#include <ISceneManager.h>
#include "utils/base_scenenode.h"

namespace lampe{
namespace xml{
class MeshSkinFactory:public IIrrXmlNode{
public:
	MeshSkinFactory(irr::IrrlichtDevice*d):IIrrXmlNode(d){}
	virtual LAMPE_VECTOR_VENDOR<String> getProcessedTags(){
		LAMPE_VECTOR_VENDOR<String> list;
		list.push_back("mesh:skin");
		return list;
	};

	virtual lampe::xml::TagResult tagHandlerBefor(lampe::xml::IXMLFactoryEnvironment*environment,lampe::xml::IXmlNode*node){
		irr::scene::ISceneNode*parent=0;

		if ((node->getParent()) && (node->getParent()->getTag())){
			parent=(irr::scene::ISceneNode*)node->getParent()->getTag();
		}

		String href=node->getAttString("href","");
		if (href=="")
			return lampe::xml::STOP;

		irr::scene::IAnimatedMesh*model=getDevice()->getSceneManager()->getMesh(href.c_str());
		if (!model)
			return lampe::xml::STOP;

		irr::scene::IAnimatedMeshSceneNode*modelNode=getDevice()->getSceneManager()->addAnimatedMeshSceneNode(model,parent);
		readBaseSceneNodeTags(modelNode,node);
		node->setTag(modelNode);
		return lampe::xml::NEXT;
	}
};

}//namespace xml
}//namespace lampe

void regMeshSkin(lampe::xml::IXMLFactoryEnvironment*environment,irr::IrrlichtDevice*d){
	lampe::xml::IFactoryRecord*h=new lampe::xml::MeshSkinFactory(d);
	environment->registerHandler(h);
	h->drop();
}